﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class RiserPipeTag : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
				Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Autodesk.Revit.DB.View activeView = document.ActiveView;
				Selection selection = activeUIDocument.Selection;
				if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
				{
					(ViewType)1,
					(ViewType)2,
					(ViewType)4
				}))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					activeView = document.ActiveView;
					if (activeView is View3D && !(activeView as View3D).IsLocked)
					{
						YJKMessageBox.Warning("该三维视图无法标注立管，请锁定视角后再进行此操作！");
						result = Autodesk.Revit.UI.Result.Cancelled;
					}
					else
					{
						transaction = new Transaction(document, "水管立管标注");
						transaction.Start();
						for (;;)
						{
							ISelectionFilter selectionFilter = new RiserPipeFilter();
							Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, selectionFilter, "请选择要标注的立管");
							Element element = document.GetElement(reference);
							XYZ pnt = selection.PickPoint("请选择标注位置的点");
							YJKRevitTools.DeleteTagOfElement(document, BuiltInCategory.OST_PipeTags, element);
							YJKRevitTools.SetSystemSimple(document, element as Pipe);
							FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(document, "水管立管标记", BuiltInCategory.OST_PipeTags);
							if (familySymbol != null)
							{
								IndependentTag independentTag = RevitVersionFuncs.NewTag(document, activeView, new Reference(element), false, 0, 0, pnt);
								independentTag.ChangeTypeId(new ElementId(familySymbol.Id.IntegerValue));
								independentTag.HasLeader = true;
							}
							else if (familySymbol == null)
							{
								string markFamilyPath = UnifiedModified.GetMarkFamilyPath(document, "HY_水管立管标记");
								document.LoadFamily(markFamilyPath);
								RevitVersionFuncs.NewTag(document, activeView, new Reference(element), false, 0, 0, pnt).HasLeader = true;
							}
						}
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() == 1)
				{
					transaction.Commit();
				}
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
